from GuiObject import *

class Gui:
	# Gui takes no init args.
	def __init__(self):
		# List of GuiObjects, the objects in this list will be drawn
		# and updated until removed from the list.
		self.guiObjects = []
		
		# Temp variable to let the gui kill itself
		self.ready = 1
			
		# This function draws all the GuiObjects in the list.
		# Objects that can contain children render the children
		# themselves.
	def draw(self):
		for obj in self.guiObjects:
			obj.draw()
			
	# Adds a GuiObject to the list
	def add(self, guiObject_):
		self.guiObjects.append(guiObject_)
		
	
	# TODO: Plenty to be done on the update loop of the gui. I think that the 
	# hover events should be called here. Also a good place to see if the content
	# in a window has been changed eg. a window displaying the details for the current
	# star.
	# Checks to see if an object should remove itself
	def update(self):
		# iter through objects
		for obj in self.guiObjects:
			# for each object, check the children of that object (if any)
			for child in obj.children:
				# make sure the children are still alive if not remove them
				if child.alive == 0:
					obj.children.remove(child)
			# check to see if the object is still alive if not remove it
			if obj.alive == 0:
				self.guiObjects.remove(obj)
	
	# Returns the GuiObject if any that the mouse pointer is in
	# TODO: should change the name of this function to better
	# represent what it does. Can be used to see what the mouse
	# is hovering over as well.
	def getClick(self, x_, y_):
		finalObj = None
		for obj in self.guiObjects:
			if obj.children:
				for child in obj.children:
					if x_ >= child.getX(): 
						if x_ <= (child.getX() + child.w):
							if y_ >= child.getY(): 
								if y_ <= child.getY() + child.h:
									return child					
			if x_ >= obj.getX(): 
				if x_ <= (obj.getX() + obj.w):
					if y_ >= obj.getY(): 
						if y_ <= obj.getY() + obj.h:
							finalObj = obj
		return finalObj
